﻿using log4net;
using log4net.Config;
using OpenApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using SuperLottery;
using System.Net;
using System.Text.RegularExpressions;

namespace WcfHostConsole
{
	class Program
	{
		static void Main(string[] args)
		{
			Crawl("2013044");

			//XmlConfigurator.Configure();
			MyLogger.Default.Error("xxx");
			Console.ReadLine();
		}

		public static Dictionary<string, object> Crawl(string issueNumber)
		{
			Dictionary<string, object> result = new Dictionary<string, object>();

			string detailUrl = "http://www.chinafcw.com.cn/ulWinInfo.do?issueNumber={0}&method=detail";
			detailUrl = String.Format(detailUrl, issueNumber);
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			string numbersPattern = @"<td height=""36"" class=""leftfont"">红球号码：</td>[\s]*<td class=""kj_img""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""></td>";
			var match = Regex.Match(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			string numbers = match.Groups.OfType<Group>().Skip(1).Select(group => group.Value).Aggregate((a, b) => a + "," + b);
			result.Add("Numbers", numbers);

			string bounsNumbersPattern = @"<td height=""36"" class=""leftfont"">蓝球号码：</td>\s*<td class=""kj_img""><img src=""/images/num/b(\d{2}).jpg"" width=""38"" height=""28""></td>";
			match = Regex.Match(htmlSource, bounsNumbersPattern, RegexOptions.IgnoreCase);
			string bounsNumbers = match.Groups[1].Value;
			result.Add("BounsNumbers", bounsNumbers);

			string amountPattern = @"本期投注金额为\s*(?<SalesAmount>\d*)\s*<br>\W*奖池资金累计为\s*(?<CumulativeAmount>\d*)\s*元";
			match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
			string strSalesAmount = match.Groups["SalesAmount"].Value;
			string strCumulativeAmount = match.Groups["CumulativeAmount"].Value;
			long salesAmount, cumulativeAmount;
			Int64.TryParse(strSalesAmount, out salesAmount);
			result.Add("SalesAmount", salesAmount);
			Int64.TryParse(strCumulativeAmount, out cumulativeAmount);
			result.Add("CumulativeAmount", cumulativeAmount);

			string winningDetailPattern = @"(?<=>)(?<Award>\w{2,}奖)(?=<\/td>)[\s\S]*?(?<=>)(?<NumberOfWinners>\d*?)(?=<\/td>)[\s\S]*?(?<=>)(?<MoneyPerWinner>\d*?)(?=<\/td>)";
			List<object[]> details = new List<object[]>();
			MatchCollection matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			foreach (Match matchItem in matches)
			{
				object[] detail = new object[3];
				detail[0] = matchItem.Groups["Award"].Value;
				string strNumberOfWinners = matchItem.Groups["NumberOfWinners"].Value;
				string strMoneyPerWinner = matchItem.Groups["MoneyPerWinner"].Value;
				int numberOfWinners, moneyPerWinner;
				if (Int32.TryParse(strNumberOfWinners, out numberOfWinners))
				{
					detail[1] = numberOfWinners;
				}
				if (Int32.TryParse(strMoneyPerWinner, out moneyPerWinner))
				{
					detail[2] = moneyPerWinner;
				}
				details.Add(detail);
			}

			result.Add("WinningDetails", details);

			return result;
		}
	}
}
